package cn.itsource.system.controllor;

import cn.itsource.system.domain.Permission;
import cn.itsource.system.query.PermissionQuery;
import cn.itsource.system.service.PermissionService;
import cn.itsource.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissonController {
    
    @Autowired
    private PermissionService permissonService;

    /**
     * 新增或修改
     * @param p
     * @return
     */
    @PostMapping
    public AjaxResult save(@RequestBody Permission p){
        if (p.getId()==null){
            permissonService.insert(p);
            return AjaxResult.ok();
        }else {
            permissonService.updateByPrimaryKeySelective(p);
            return AjaxResult.ok();
        }
    }
    



    /**
     * 分页查询
     * @param permissionQuery
     * @return
     */
    @PatchMapping
    public AjaxResult pageInfo(@RequestBody PermissionQuery permissionQuery){
        try {
            return AjaxResult.ok(permissonService.pageInfo(permissionQuery));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 批量删除
     * @param p
     * @return
     */
    @PatchMapping("/batchDeletion")
    public AjaxResult batchDeletion(@RequestBody List<Permission> p){
        try {
            permissonService.deleteAll(p);
            return AjaxResult.ok();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            permissonService.deleteByPrimaryKey(id);
            return AjaxResult.ok();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 树形展示
     * @return
     */
    @PatchMapping("/gettree")
    public AjaxResult result(){
        try {
            return AjaxResult.ok(permissonService.gettree());
        } catch (Exception e) {
            throw new RuntimeException(e);
    }
    }

}
